package com.fan.client;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Map;

@Component
public class ConfigContext {

    private RestTemplate restTemplate;

    public void initRestTemplate() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);//单位为ms
        factory.setConnectTimeout(5000);//单位为ms
        RestTemplate restTemplate = new RestTemplate(factory);
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8")));
        this.restTemplate = restTemplate;
    }

    public Map<String, String> getProperty() {
        initRestTemplate();
        PropertyConfig propertyConfig = ConfigManager.INSTANCE.getPropertyConfig();
        String profile = propertyConfig.getProfile();
        String configUrl = propertyConfig.getConfigUrl();
        String appName = propertyConfig.getAppName();
        if (StringUtils.isBlank(profile) || StringUtils.isBlank(configUrl) || StringUtils.isBlank(appName)) {
            System.out.println(restTemplate);
        }
        restTemplate.getForObject(configUrl.concat("/get/{1}/{2}"), Map.class, 1, 5);
        return null;
    }
}
